﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using EasyStudy.Logic;
using EasyStudy.Properties;
using System.IO;
using ComponentFactory.Krypton.Toolkit;

namespace EasyStudy.UI
{
    public partial class FrmAddTask : KryptonForm
    {

        private List<DateTimePicker> dateTimePickers;
        private List<Label> lblWeekdays;

        public DateTime getRemindTimeIgnoreWeekend(DateTime dateTime) {
            if (chkIgnoreWeekend.Checked)
            {
                if (dateTime.DayOfWeek == DayOfWeek.Saturday)
                {
                    dateTime = dateTime.AddDays(2);
                }
                else if (dateTime.DayOfWeek == DayOfWeek.Sunday)
                {
                    dateTime = dateTime.AddDays(1);
                }
            }

            return dateTime;
        }

        public void setWeekTable(Label lbl, DayOfWeek dayOfWeek) {
            lbl.Text = getWeek4Display(dayOfWeek);

            if (dayOfWeek == DayOfWeek.Saturday || dayOfWeek == DayOfWeek.Sunday)
            {
                lbl.BackColor = Color.FromArgb(201, 48, 44);
                lbl.ForeColor = Color.White;
            }
            else {
                lbl.BackColor = Color.FromArgb(92, 184, 92);
                lbl.ForeColor = Color.Black;
            }
        }

        public static String getWeek4Display(DayOfWeek dayOfWeek) {
            String result = "";

            switch (dayOfWeek) { 
                case DayOfWeek.Monday:
                    result = "星期一";
                    break;
                case DayOfWeek.Tuesday:
                    result = "星期二";
                    break;
                case DayOfWeek.Wednesday:
                    result = "星期三";
                    break;
                case DayOfWeek.Thursday:
                    result = "星期四";
                    break;
                case DayOfWeek.Friday:
                    result = "星期五";
                    break;
                case DayOfWeek.Saturday:
                    result = "星期六";
                    break;
                case DayOfWeek.Sunday:
                    result = "星期天";
                    break;
            }

            return result;
        }

        public FrmAddTask()
        {
            InitializeComponent();
            initComponentLists();

            Icon = Resources.task1;
            dateTimePicker.Format = DateTimePickerFormat.Custom;
            dateTimePicker.CustomFormat = "yy-MM-dd HH:mm";
            loadCategories();
            loadTimerCircle();
            dateTimePicker.ValueChanged += new EventHandler(dateTimePicker_ValueChanged);

            kryptonTaskDialog1.WindowTitle = "恭喜";
            kryptonTaskDialog1.Content = "添加任务成功！";
            kryptonTaskDialog1.Icon = MessageBoxIcon.Information;
            kryptonTaskDialog1.CommonButtons = TaskDialogButtons.OK;

            // 根据配置文件设置自动跳过周末
            this.chkIgnoreWeekend.Checked = Configuration.GetInstance().IsIgnoreWeekend;
        }

        /// <summary>
        /// 初始化组件数组
        /// </summary>
        private void initComponentLists() {

            dateTimePickers = new List<DateTimePicker>();
            lblWeekdays = new List<Label>();

            dateTimePickers.Add(dateTimePicker1);
            dateTimePickers.Add(dateTimePicker2);
            dateTimePickers.Add(dateTimePicker3);
            dateTimePickers.Add(dateTimePicker4);
            dateTimePickers.Add(dateTimePicker5);
            dateTimePickers.Add(dateTimePicker6);
            dateTimePickers.Add(dateTimePicker7);
            dateTimePickers.Add(dateTimePicker8);
            dateTimePickers.Add(dateTimePicker9);
            dateTimePickers.Add(dateTimePicker10);
            dateTimePickers.Add(dateTimePicker11);
            dateTimePickers.Add(dateTimePicker12);
            dateTimePickers.Add(dateTimePicker13);

            lblWeekdays.Add(lblWeek1);
            lblWeekdays.Add(lblWeek2);
            lblWeekdays.Add(lblWeek3);
            lblWeekdays.Add(lblWeek4);
            lblWeekdays.Add(lblWeek5);
            lblWeekdays.Add(lblWeek6);
            lblWeekdays.Add(lblWeek7);
            lblWeekdays.Add(lblWeek8);
            lblWeekdays.Add(lblWeek9);
            lblWeekdays.Add(lblWeek10);
            lblWeekdays.Add(lblWeek11);
            lblWeekdays.Add(lblWeek12);
            lblWeekdays.Add(lblWeek13);

            // 注册事件
            for (int i = 0; i < dateTimePickers.Count; ++i) {
                dateTimePickers[i].Tag = i;
                dateTimePickers[i].ValueChanged += new EventHandler(FrmAddTask_ValueChanged);
            }
        }

        void FrmAddTask_ValueChanged(object sender, EventArgs e)
        {
            DateTimePicker dateTimePicker = (DateTimePicker)sender;
            int index = (int)dateTimePicker.Tag;
            setWeekTable(lblWeekdays[index], dateTimePicker.Value.DayOfWeek);
        }

        private void loadTimerCircle()
        {
            EbbinghausTask task = ebbTaskMgr.CreateAEbbTask(
                   txtSubject.Text,
                   txtComment.Text,
                   cmbType.Text,
                   dateTimePicker.Value);

            int i = 0;
            if (i < task.RemindTimes.Count)
            {
                DateTime curRemindTime = 
                task.RemindTimes[i] = this.dateTimePicker1.Value = getRemindTimeIgnoreWeekend(task.RemindTimes[i]);
                setWeekTable(lblWeek1, this.dateTimePicker1.Value.DayOfWeek);
                ++i; 
            }
            if (i < task.RemindTimes.Count)
            {
                this.dateTimePicker2.Value = getRemindTimeIgnoreWeekend(task.RemindTimes[i]);
                ++i;
                setWeekTable(lblWeek2, this.dateTimePicker2.Value.DayOfWeek);
            }
            if (i < task.RemindTimes.Count)
            {
                this.dateTimePicker3.Value = getRemindTimeIgnoreWeekend(task.RemindTimes[i]);
                ++i;
                setWeekTable(lblWeek3, this.dateTimePicker3.Value.DayOfWeek);
            }
            if (i < task.RemindTimes.Count)
            {
                this.dateTimePicker4.Value = getRemindTimeIgnoreWeekend(task.RemindTimes[i]);
                ++i;
                setWeekTable(lblWeek4, this.dateTimePicker4.Value.DayOfWeek);
            }
            if (i < task.RemindTimes.Count)
            {
                this.dateTimePicker5.Value = getRemindTimeIgnoreWeekend(task.RemindTimes[i]);
                ++i;
                setWeekTable(lblWeek5, this.dateTimePicker5.Value.DayOfWeek);
            }
            if (i < task.RemindTimes.Count)
            {
                this.dateTimePicker6.Value = getRemindTimeIgnoreWeekend(task.RemindTimes[i]);
                ++i;
                setWeekTable(lblWeek6, this.dateTimePicker6.Value.DayOfWeek);
            }
            if (i < task.RemindTimes.Count)
            {
                this.dateTimePicker7.Value = getRemindTimeIgnoreWeekend(task.RemindTimes[i]);
                ++i;
                setWeekTable(lblWeek7, this.dateTimePicker7.Value.DayOfWeek);
            }
            if (i < task.RemindTimes.Count)
            {
                this.dateTimePicker8.Value = getRemindTimeIgnoreWeekend(task.RemindTimes[i]);
                ++i;
                setWeekTable(lblWeek8, this.dateTimePicker8.Value.DayOfWeek);
            }
            if (i < task.RemindTimes.Count)
            {
                this.dateTimePicker9.Value = getRemindTimeIgnoreWeekend(task.RemindTimes[i]);
                ++i;
                setWeekTable(lblWeek9, this.dateTimePicker9.Value.DayOfWeek);
            }
            if (i < task.RemindTimes.Count)
            {
                this.dateTimePicker10.Value = getRemindTimeIgnoreWeekend(task.RemindTimes[i]);
                ++i;
                setWeekTable(lblWeek10, this.dateTimePicker10.Value.DayOfWeek);
            }
            if (i < task.RemindTimes.Count)
            {
                this.dateTimePicker11.Value = getRemindTimeIgnoreWeekend(task.RemindTimes[i]);
                ++i;
                setWeekTable(lblWeek11, this.dateTimePicker11.Value.DayOfWeek);
            }
            if (i < task.RemindTimes.Count)
            {
                this.dateTimePicker12.Value = getRemindTimeIgnoreWeekend(task.RemindTimes[i]);
                ++i;
                setWeekTable(lblWeek12, this.dateTimePicker12.Value.DayOfWeek);
            }
            if (i < task.RemindTimes.Count)
            {
                this.dateTimePicker13.Value = getRemindTimeIgnoreWeekend(task.RemindTimes[i]);
                ++i;
                setWeekTable(lblWeek13, this.dateTimePicker13.Value.DayOfWeek);
            }
        }

        private void loadCategories()
        {
            cmbType.Text = "";
            cmbType.Items.Clear();
            cmbType.Items.AddRange(EbbTaskDBMgr.getAllCategories().ToArray());

            if (cmbType.Items.Count > 0)
            {
                cmbType.SelectedIndex = 0;
                cmbType.Select(0, 0);
            }
        }

        void dateTimePicker_ValueChanged(object sender, EventArgs e)
        {
            loadTimerCircle();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (validate())
            {
                EbbinghausTask task = ebbTaskMgr.CreateAEbbTask(
                    txtSubject.Text,
                    txtComment.Text,
                    cmbType.Text,
                    dateTimePicker.Value);
                List<DateTime> remindTimes = new List<DateTime>();

                remindTimes.Clear();
                remindTimes.Add(this.dateTimePicker1.Value);
                remindTimes.Add(this.dateTimePicker2.Value);
                remindTimes.Add(this.dateTimePicker3.Value);
                remindTimes.Add(this.dateTimePicker4.Value);
                remindTimes.Add(this.dateTimePicker5.Value);
                remindTimes.Add(this.dateTimePicker6.Value);
                remindTimes.Add(this.dateTimePicker7.Value);
                remindTimes.Add(this.dateTimePicker8.Value);
                remindTimes.Add(this.dateTimePicker9.Value);
                remindTimes.Add(this.dateTimePicker10.Value);
                remindTimes.Add(this.dateTimePicker11.Value);
                remindTimes.Add(this.dateTimePicker12.Value);
                remindTimes.Add(this.dateTimePicker13.Value);

                task.RemindTimes = remindTimes;
                task.NextTime = this.dateTimePicker1.Value;
                EbbTaskDBMgr.AddAEbbTaskToDB(task);
                kryptonTaskDialog1.Content = "添加新任务成功";
                kryptonTaskDialog1.ShowDialog();
            }
            else
            {
                kryptonTaskDialog1.Icon = MessageBoxIcon.Error;
                kryptonTaskDialog1.Content = "请填写任务的主题！";
                kryptonTaskDialog1.ShowDialog();
            }
        }

        private bool validate()
        {
            bool result = false;

            if (txtSubject.Text.Trim() == String.Empty)
            {
                result = false;
            }
            else
            {
                result = true;
            }

            return result;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private EbbTaskMgr ebbTaskMgr = new EbbTaskMgr();

        private void chkIgnoreWeekend_CheckedChanged(object sender, EventArgs e)
        {
            loadTimerCircle();
        }
    }
}
